from django.db import models
from networkx.db import models as model

from networkx.lib import stdlib

# Create your models here.
#
IP_VERSION = ( (4, 'IPv4'), (6, 'IPv6'), )

class Site(model.BaseModel):
   site = models.CharField(max_length = 75, help_text = "Site name",
         unique = True)
   location = models.CharField(max_length = 255, help_text = "Site Location")
   contact = models.CharField(max_length = 100, help_text = "Site contact")

   class Meta:
      db_table = u'site'
      ordering = ['site']

   def __str__(self):
      return stdlib.my_str(self.site)

class Domain(model.BaseModel):
   site = models.ForeignKey(Site)
   name = models.CharField(max_length = 75, help_text = "DNS Domain Name")
   primary = model.IPAddressField(help_text = "Primary DNS server IP")
   secondary = model.IPAddressField(help_text = "Secondary DNS server IP")

   class Meta:
      db_table = u'dns_servers'

   def str(self):
      return "<%s: %s>" %(self.site, stdlib.my_str(self.name))

class Ntp(model.BaseModel):
   site = models.ForeignKey(Site)
   primary = model.IPAddressField(help_text = "Primary NTP server IP")
   secondary = model.IPAddressField(help_text = "Secondary NTP server IP")

   class Meta:
      db_table = u'ntp_servers'

   def str(self):
      return "<%s: ntp>" %self.site

class Syslog(model.BaseModel):
   site = models.ForeignKey(Site)
   primary = model.IPAddressField(help_text = "Primary Syslog server IP")
   secondary = model.IPAddressField(help_text = "Secondary Syslog server IP")

   class Meta:
      db_table = u'syslog_servers'

   def str(self):
      return "<%s: syslog>" %self.site

class Tacacs(model.BaseModel):
   key = model.CharField(max_length = 75, help_text = 'tacacs key',
         unique = True)
   primary = model.IPAddressField(help_text = "Primary Syslog server IP")
   secondary = model.IPAddressField(help_text = "Secondary Syslog server IP")

   class Meta:
      db_table = u'tacacs_servers'

class Global(model.BaseModel):
   site = models.ForeignKey(Site)
   domain = models.ForeignKey(Domain)
   ntp = models.ForeignKey(Ntp)
   syslog = models.ForeignKey(Syslog)
   tacacs = models.ForeignKey(Tacacs)
   snmpro = models.CharField(max_length = 15, help_text = "SNMP R/O string")
   rootpw = foo
   enopspw = bar
   enablepw = goo

   class Meta:
      db_table = u'global'

class HashInfo(model.BaseModel):
   junos_root = models.CharField(max_length = 25,
         help_text = "JunOS Root password")
   junos_enops = models.CharField(max_length = 25,
         help_text = "JunOS EnOps (remote) user password")
   dell_enable = models.CharField(max_length = 25,
         help_text = "Dell Enable password"),
   dell_enops = models.CharField(max_length = 25,
         help_text = "Dell Enops user password")

   class Meta:
      db_table = u'hashinfo'

class IPBlock(model.BaseModel):
   address = model.IPField(help_text = "Address field"),
   version = model.IntegerField(help_text = "IP Address Version",
         choices = IP_VERSION)
   prefix = models.IntegerField(help_text = "IP subnet prefix")

   class Meta:
      db_table = u'ipblock'

class Console(model.BaseModel):
   site = models.ForeignKey(Site)
   primary = model.IPAddressField(help_text = "Primary Console server IP")
   primary_cons = models.IntegerField(null = False, name = "conport1",
         help_text = "Primary console port number")
   secondary = model.IPAddressField(help_text = "Secondary Console server IP")
   secondary_cons = models.IntegerField(null = False, name = "conport2",
         help_text = "Secondary console port number")

   class Meta:
      db_table = u'console'

class Switch(model.BaseModel):
   site = models.ForeignKey(Site)
   console = models.ForeignKey(Console)
   gdata = models.ForeignKey(Global)
   name = models.CharField(max_length = 75, help_text = "Switch name")
   address = model.ForeignKey(IPBlock)
   gateway = model.IPAddressField(help_text = "IPv4 gateway for the switch")
   mgmt = models.BooleanField(help_text = "Management switch",
         name = "mgmt")
   hashinfo = models.ForeignKey(HashInfo)

   class Meta:
      db_table = u'switch'

   IsManagement = property(fget = lambda self: bool(self.mgmt))

   def __str__(self):
      return stdlib.my_str(self.name)

class Vlan(model.BaseModel):
   site = models.ForeignKey(Site)
   name = models.CharField(help_text = "VLAN name", max_length = 25)
   vlanid = models.IntegerField(help_text = "VLAN Id")
   subnet = models.ForeignKey(IPBlock)

   class Meta:
      db_table = u'vlan'

   def __str__(self):
      return stdlib.my_str(self.name)

class AggrSwitch(model.BaseModel)

